<template>
  <div class="employees">
    <el-card>
      <pageTools :show-left="false">
        <template #right>
          <div>
            <el-button type="primary" @click="goUpload">导入</el-button>
            <el-button type="primary" @click="addClick">+ 新增员工</el-button>
          </div>
        </template>
      </pageTools>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column
          label="聘用形势"
          prop="formOfEmployment"
          :formatter="formatter"
          width="80"
        />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column
          label="入职时期"
          prop="timeOfEntry"
          :formatter="formatterTimeOfEntry"
        />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <!-- current-page:默认页码
             page-sizes:页容量选项 默认页容量一定要在页容量选项中存在
             page-size:默认页容量
             layout：total总数 sizes页容量 prev上一页 pager：页码 next下一页 jumper：跳转到哪一页
             total：总数
             size-change:页容量改变
             current-change：页码改变
             -->
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 3, 10, 100, 200, 300, 400]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增组件 -->
    <Add ref="add" @getData="getData" />
  </div>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { sysUser, sysUserDelete } from '@/api/employees.js'
import Add from './components/add.vue'
import dayjs from 'dayjs'
export default {
  components: {
    Add
  },
  data() {
    return {
      list: [],
      page: {
        page: 1, // 是 1 页码
        size: 4, // 是4 即每页4条
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 页容量改变
    handleSizeChange(size) {
      this.page.pagesize = size
      // 页容量改变 页码最好回到第一页
      this.page.page = 1
      this.getData()
    },
    // 页码改变
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 获取列表
    async getData() {
      const res = await sysUser(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log('员工列表', res)
    },
    // 格式化聘用形势
    formatter(row, column, cellValue, index) {
      // array.find(查找 满足条件的想 找到了就返回找到项数据 找不到返回undefined)
      const temp = employeesData.hireType.find((item) => {
        return item.id === +cellValue
      })
      return temp ? temp.value : '-'
    },
    // 数据转换
    formatterTimeOfEntry(row, column, cellValue, index) {
      console.log(cellValue)
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    // 新增点击事件
    addClick() {
      // 打开组件弹框
      this.$refs.add.dialogVisible = true
    },
    // 删除员工
    del(row) {
      this.$confirm('确定删除该员工吗？', '提示').then(async() => {
        await sysUserDelete(row.id)
        this.$message.success('删除成功！')
        // 完成刷新列表数据回到第一页
        this.handleCurrentChange(1)
      })
    },
    // 导入点击 跳转页面 传入了name参数
    goUpload() {
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工')) // 转码
    },
    // 查看跳转 跳转页面并完成动态路由匹配传参
    look(row) {
      // console.log(row)
      this.$router.push('/employees/detail/' + row.id)
    }
  }
}
</script>
